#include "TcpServer.h"

TcpServer::TcpServer(const uint16_t port)
{
    Sock* serverSock = new Sock();
    // 服务器的只需要这个port就行
    InetAddress servaddr(port);
    // 创建非阻塞的sock
    serverSock->cerateNonBlockingSocket();
    serverSock->setKeepAlive(true);
    serverSock->setReuseAddr(true);
    serverSock->setReusePort(true);
    serverSock->settcpnodelay(true);
    serverSock->bind(servaddr);
    serverSock->listen();

    Channel *servchannel = new Channel(&loop_, serverSock->getSock());
    servchannel->setReadCallBack(std::bind(&Channel::newconnection,servchannel,serverSock));
    servchannel->enableReading();
}
TcpServer::~TcpServer()
{}

void TcpServer::start()
{
    loop_.run();
}